package edu.uic.kott.fragments;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.activities.TrackKeyOutcomes;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.ClassActivity;
import edu.uic.kott.model.Course;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

public class TrackActivities extends Fragment {
	
	static TextView classActivitiesOn;
	static String activitiesDate;
	
	Button activitiesRefreshButton;
	
	private static TableLayout activitiesQueryLayout;
	
	KottDataSource datasource;
	List<ClassActivity> activityList;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View track_activities_view = inflater.inflate(R.layout.track_activities, container, false);
		
		classActivitiesOn = (TextView) track_activities_view.findViewById(R.id.classActivitiesOn);
		classActivitiesOn.setText(TrackKeyOutcomes.activeDate);
		
		activitiesRefreshButton = (Button) track_activities_view.findViewById(R.id.activitiesRefreshButton);
		activitiesRefreshButton.setOnClickListener(activitiesRefreshListener);
		
		activitiesQueryLayout = (TableLayout) track_activities_view.findViewById(R.id.activitiesQueryLayout);
		
		TrackKeyOutcomes.activitiesVisible = true;
		TrackKeyOutcomes.UpdateVariables();
		
		ActivitiesData();
		
		return track_activities_view;
	}

	public OnClickListener activitiesRefreshListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			removeAllViews();
			ActivitiesData();
			
		}
		
	};
	
	public static void removeAllViews() {
		activitiesQueryLayout.removeAllViews();
	}
	
	private void ActivitiesData() {
		datasource = new KottDataSource(this.getActivity());
		
		ClassActivity activity = new ClassActivity();

		List<ClassActivity> activitiesList = new ArrayList<ClassActivity>(); 
		activitiesList = datasource.getAllActivities();

		for (ClassActivity a : activitiesList) {
			String id = String.valueOf(a.getActivityId());
			
			activity = datasource.getActivityDetails(id);
			
			String className = activity.getCourse();
			String type = activity.getActivityType();
			String activityName = activity.getActivityName();
			String dueDate = activity.getDueDate();
			String description = activity.getActivityDescription();
			String isCompleted = activity.isCompleted();
			int index = Integer.parseInt(id) - 1;
			
			Log.i("IF", dueDate + " = " + activitiesDate);
			
			SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
			Date due = Calendar.getInstance().getTime();
			Date today = Calendar.getInstance().getTime();
			
			try {
				due = df.parse(dueDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			try {
				today = df.parse(activitiesDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			if (due.compareTo(today) == 0) {
				Log.i("Completed?", isCompleted);
				makeActivitiesGUI(id, className, type, activityName, dueDate, description, isCompleted, index);	
			}
			
		}
		
		//Above should end up looping like this
		//makeActivitiesGUI(0, "IDS406", "final", "KOTT app", "5/1/14", "Key Outcomes Tracker Tool Application", "true", 0);
		//makeActivitiesGUI(1, "IDS495", "presentation", "Capsim", "4/29/14", "Capsim Presentation", "false", 1);
	}
	
	public void makeActivitiesGUI(String activityId, String className, String activityType, String activityName, String dueDate, 
			String description, String isCompleted, int index) {
		LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View newRowView = inflater.inflate(R.layout.activities_group_view, null);
		
		TextView classText = (TextView) newRowView.findViewById(R.id.activitiesClassName);
		classText.setText(className);
		
		TextView typeText = (TextView) newRowView.findViewById(R.id.activitiesType);
		typeText.setText(activityType);
		
		TextView nameText = (TextView) newRowView.findViewById(R.id.activitiesName);
		nameText.setText(activityName);
		
		TextView dueText = (TextView) newRowView.findViewById(R.id.activitiesDueDate);
		dueText.setText(dueDate);
		
		TextView descText = (TextView) newRowView.findViewById(R.id.activitiesDescription);
		descText.setText(description);
		
		Log.i("VALUE", "of isCompleted = " + isCompleted);
		
		Boolean completed = false;
		if (isCompleted == "true") {
				completed = true;
		}
		CheckBox completedCheckBox = (CheckBox) newRowView.findViewById(R.id.activitiesCompletedCheckBox);
		if (completed) {
			completedCheckBox.setChecked(true);
			completedCheckBox.setFocusable(false);
			completedCheckBox.setClickable(false);
		}
		else {
			completedCheckBox.setOnCheckedChangeListener(completedListener);
		}
		
		newRowView.setTag(activityId);
		
		activitiesQueryLayout.addView(newRowView, index);
	}
	
	public OnCheckedChangeListener completedListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			//Completed
			LinearLayout group = (LinearLayout) buttonView.getParent().getParent();
			
			String activityId = String.valueOf(group.getTag());
			
			datasource.markActivityComplete(activityId);
			Log.i("MARKED", activityId + " was marked completed");
		
			CheckBox completed = (CheckBox) group.findViewById(R.id.activitiesCompletedCheckBox);
			completed.setFocusable(false);
			completed.setClickable(false);
		}
		
	};

	public static void ActivitiesUpdateOnDateChange() {
		removeAllViews();
		activitiesDate = TrackKeyOutcomes.activeDate;
		classActivitiesOn.setText("on: " + activitiesDate);
	}
	
	@Override
	public void onStop() {
		TrackKeyOutcomes.activitiesVisible = false;
		super.onStop();
	}
	
	@Override
	public void onResume() {
		TrackKeyOutcomes.activitiesVisible = true;
		super.onResume();
	}
}